package com.maimao.goods.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.maimao.api.mall.goods.CateApi;
import com.maimao.core.constant.Constants;
import com.maimao.core.enums.RespCode;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.CateService;
import com.maimao.model.goods.Category;
import com.maimao.model.goods.input.CateSearch;
import com.maimao.model.goods.input.CategoryAddBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类
 *
 * @author MaoLin Wang
 * @date 2020/12/11 3:18 下午
 */
@RestController
public class CateController extends BaseController implements CateApi {

    @Autowired
    private CateService cateService;


    /**
     * 后台分类管理查询过滤
     */
    @Override
    @PostMapping("/ms/search")
    public Result<?> msSearch(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                 @RequestBody CateSearch cateSearch) {
        PageResult<Category> pageResult = cateService.searchData(page, size, cateSearch);
        return Result.okData(pageResult);
    }

    /**
     * 添加分类
     *
     * @param categoryAddBean 添加分类入参bean
     * @return 添加结果
     */
    @PostMapping("/ms/add")
    @Override
    public Result<?> addCate(@Valid @RequestBody CategoryAddBean categoryAddBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        Result<?> result = this.validCateBean(categoryAddBean);
        if (!ObjectUtil.equal(result, null)) {
            return result;
        }
        categoryAddBean.setCreateTime(DateUtils.now());
        categoryAddBean.setUpdateTime(DateUtils.now());
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddBean, category);
        cateService.add(category, categoryAddBean.getDeletePics());
        return Result.ok("添加成功");
    }

    /**
     * 修改分类
     *
     * @param categoryAddBean 修改分类入参bean
     * @return 修改结果
     */
    @PostMapping("/ms/update")
    @Override
    public Result<?> updateCate(@Valid @RequestBody CategoryAddBean categoryAddBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        if (categoryAddBean.getId() == null || categoryAddBean.getId() <= 0) {
            return Result.notValid();
        }
        Result<?> result = this.validCateBean(categoryAddBean);
        if (!ObjectUtil.equal(result, null)) {
            return result;
        }
        categoryAddBean.setUpdateTime(DateUtils.now());
        Category category = new Category();
        BeanUtil.copyProperties(categoryAddBean, category);
        cateService.update(category, categoryAddBean.getDeletePics());
        return Result.ok("更新成功");
    }

    /**
     * 暂停分类
     *
     * @param id 分类id
     */
    @Override
    @PostMapping("/ms/stop")
    public Result<?> stop(@RequestParam(value = "id") Integer id) {
        if (id == null || id <= 0) {
            return Result.notValid("请选择分类");
        }
        cateService.updateStatus(id, 0);
        return Result.ok("已停用");
    }

    /**
     * 开启分类
     *
     * @param id 分类ID
     */
    @Override
    @PostMapping("/ms/start")
    public Result<?> start(@RequestParam(value = "id") Integer id) {
        if (id == null || id <= 0) {
            return Result.notValid("请选择分类");
        }
        cateService.updateStatus(id, 1);
        return Result.ok("开启成功");
    }

    /**
     * 分类树
     */
    @Override
    @GetMapping("/treeData")
    public Result<?> treeData() {
        return Result.okData(RespCode.OK, cateService.treeData());
    }

    /**
     * 根据关键字搜搜id列表
     *
     * @param key 关键字
     */
    @Override
    @GetMapping("/search-key")
    public Result<?> searchIdsByKey(@RequestParam("key") String key) {
        if (StringUtils.isEmpty(key)) {
            return Result.okData(RespCode.OK, null);
        }

        return Result.okData(cateService.searchIdsByKey(key));
    }

    @Override
    @PostMapping("/client/names")
    public Result<?> selectNamesByIds(@RequestBody List<Integer> ids) {
        if (ids == null || ids.size() < 1) {
            return Result.notValid();
        }
        for (Integer id : ids) {
            if (id < 1) {
                return Result.notValid();
            }
        }

        return Result.okData(cateService.selectNamesByIds(ids));
    }

    @Override
    @GetMapping("/client/batch")
    public Result<?> selectByIds(@RequestBody List<Integer> ids) {
        if (ids == null || ids.size() <= 0) {
            return Result.notValid();
        }
        return Result.okData(cateService.selectBatch(ids));
    }

    private Result<?> validCateBean(CategoryAddBean bean) {
        if (ObjectUtil.equal(bean.getLevel(), Constants.ONE)) {
            if (StrUtil.isEmpty(bean.getMobileName())) {
                return Result.notValid("请填写手机端名称");
            }
        } else if (ObjectUtil.equal(bean.getLevel(), Constants.THREE)) {
            if (StrUtil.isEmpty(bean.getPicture())) {
                return Result.notValid("请先上传手机端图片");
            }
        }
        return null;
    }


}
